/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.hazardsource.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.koron.ebs.mybatis.EnvSource;

import com.koron.hazardsource.domain.PracticeTemplate;


public interface PracticeTemplateMapper {
	
	@Insert("insert into practice_template(id,add_user,add_user_name,add_time,name,serial,template_type,structure_code,pid,remark) "
			+ "values (#{id},#{addUser},#{addUserName},#{addTime},#{name},#{serial},#{templateType},#{structureCode},#{pid},#{remark})")
	Integer insert(PracticeTemplate bean);
	
	@Select("select * from practice_template where structure_code=#{code} and template_type=#{type}")
	PracticeTemplate queryByCode(@Param("code")String code,@Param("type")String templateType);
	
	@Select("select a.*,b.`name` pname from practice_template  a left join practice_template b on a.pid=b.id where a.id=#{id}")
	PracticeTemplate queryById(@Param("id")String id);
	
	@Select("select count(*) from practice_template where pid=#{id}")
	Integer subsetCount(@Param("id")String id);
	
	@Delete("delete from practice_template where id=#{id}")
	Integer delete(@Param("id")String id);
	
	@Select("<script>"
			+ "select count(*) from practice_template where template_type=#{type} and pid"
			+ "<choose><when test='pid==null'>  is NULL</when>"
			+ "<otherwise> =#{pid}</otherwise></choose>"
			+ "</script>")
	Integer serialQuery(@Param("type")String templateType,@Param("pid")String pid);
	
	@Select("select count(*) from practice_template where pid=#{pid} and name like #{name}")
	Integer nameCount(@Param("pid")String pid,@Param("name")String name);
	
	@Select("select * from practice_template where pid=#{pid} and name like #{name}")
	List<PracticeTemplate> queryByName(@Param("pid")String pid,@Param("name")String name);
	
	@Select("select * from practice_template where pid=#{pid} order by serial")
	List<PracticeTemplate> queryByPid(@Param("pid")String pid);
	
	@Update("update practice_template set name=#{name},serial=#{serial},subnumber=#{subnumber},remark=#{remark},update_user=#{updateUser},update_user_name=#{updateUserName},update_time=#{updateTime} where id=#{id}")
	Integer update(PracticeTemplate bean);
	
	@Select("<script>"
			+ "select * from practice_template where template_type=#{type} "
			+ "<if test='id == null'> and pid is NULL</if>"
			+ "<if test='id !=null'> and id=#{id}</if>"
			+ "</script>")
	List<PracticeTemplate> queryByIdOrType(@Param("type")String templateType,@Param("id")String id);
	
	@Select("select * from practice_template where template_type=#{type} order by structure_code")
	List<PracticeTemplate> queryByType(@Param("type")String templateType);
	
	
}
